package dnd.controller
{
	import dnd.extension.base.*;
	import dnd.extension.data.SubtitleItem;
	import dnd.model.*;
	import dnd.view.SlideView;
	import dnd.view.WelcomeView;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	/**
	 * ...
	 * @author Hugh Hoang
	 */
	public class ScreenController extends ControllerBase
	{
		/**
		 * Welcome action
		 */
		public function Welcome():void
		{
			//Create the model
			var model:WelcomeModel = new WelcomeModel();
			model.greeting = "Kính chào quí khách";
			
			//Create the view
			var view:WelcomeView = new WelcomeView(model);
			
			//Project the view
			View(view);
		}
		
		/**
		 * Start the slide show
		 */
		public function BeginSlide():void
		{
			//Load the subtitle XML file
			var xmlLoader:URLLoader = new URLLoader();
			xmlLoader.addEventListener(Event.COMPLETE, loadSubtitle);
			xmlLoader.load(new URLRequest("Subtitle.xml"));
		}
		
		/**
		 * Load the subtitle XML file
		 * @param	evt
		 */
		private function loadSubtitle(evt:Event):void
		{
			//Create the XML object
			var xmlData:XML = new XML(evt.target.data);
			//Create the model
			var model:SlideModel = new SlideModel();
			//Load model subtitles
			for each (var sub:XML in xmlData.subtitle)
			{
				//Subtitle item
				var subtitle:SubtitleItem = new SubtitleItem();
				subtitle.LoadXML(sub);
				//Add to the collection
				model.subtitleCollection.push(subtitle);
			}
			
			//Create the view
			var view:SlideView = new SlideView(model);
			
			//Project the view
			View(view)
			dispatchEvent(new Event(Event.COMPLETE));
		}
	}
}